var fpcx = {
    setBtnStatus: function () {
        var row = $('#grid_fpcx').datagrid('getSelected');

    },
    alwayShowDetail: function (rowData) {
        $('#layout_fpcx').layout('remove', 'center');
        var row = $('#grid_fpcx').datagrid('getSelected');
       if (row.type == 1) {
            $('#layout_fpcx').layout('add', {
                region: 'center',
                split: true,
                href: $.webPath() + '/jsgl/fpcx/fpcx_xsjsd_detail?fpxsjsdId=' + rowData.id
            });
        }
    },
    showDetail: function (rowData) {
        if (rowData && $("#layout_fpcx").val() != rowData.id) {
            fpcx.alwayShowDetail(rowData);
        }
    }
}
$(function () {

    $("#fpcx_glqy").gsjgzcombobox();
    //表格初始加载
    $('#grid_fpcx').datagrid({
        url: $.webPath() + "/jsgl/fpcx/fpcxList",
        toolbar: "#toolbar_fpcx",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        pageNumber: 1,
        fit: true,
        rownumbers: true,
        striped: true,
        columns: [
            [
                {field: 'id', title: 'ID', hidden: true},
                {field: 'jsd_id', title: 'jsd_id', hidden: true},
                {field: 'jsdh', title: '结算单号', width: 40, align: 'center'},
                {field: 'sqrq', title: '申请日期', width: 30, align: 'center'},
                {field: 'type', title: '类型', width: 15, align: 'center',
                    formatter: function (value) {
                        if (value == '0')
                            return '采购';
                        if (value == '1')
                            return '销售';
                        if (value == '2')
                            return '物流';
                    }
                },
                {field: 'glqy_qc', title: '客户名称', width: 30, align: 'center'},
                {field: 'glqy_qyfr_nsh', title: '纳税号', width: 35, align: 'center'},
                {field: 'fpze', title: '发票总额(元)', width: 30, align: 'right',
                    formatter: function (value) {
                        return Math.round(value * 100) / 100;
                    },
                    formatter: function (value) {
                        var sl = formatJe2(value, ",");
                        return sl;
                    }
                },
                {field: 'ddfy', title: '代垫运费(元)', width: 30, align: 'right',
                    formatter: function (value) {
                        var sl = formatJe2(value, ",");
                        return sl;
                    }
                },
                {field: 'fpzl', title: '发票种类', width: 18, align: 'center',
                    formatter: function (value) {
                        if (value == '0')
                            return '增票';
                        if (value == '1')
                            return '普票';
                        if (value == '2')
                            return '其他';
                        if (value == '3')
                            return '另开票';
                        if (value == '4')
                            return '物流发票';
                    }
                },
                {field: 'hm', title: '发票号码', width: 35, align: 'center'},
                {field: 'sqr_xm', title: '申请人', width: 15, align: 'center'},
                {field: 'kprq', title: '开票日期', width: 30, align: 'center'},
                {field: 'kpr_xm', title: '开票人', width: 15, align: 'center'},
                {field: 'gs_jc', title: '申报公司', width: 30, align: 'center'},
                {field: 'cggs_jc', title: '业务公司', width: 30, align: 'center'},
                {field: "zt", title: "状态", width: 25, align: 'center',
                    formatter: function (value) {
                        if (value == '0')
                            return '未抛账';
                        if (value == '1')
                            return '已退回';
                        if (value == '2')
                            return '已抛账';
                        if (value == '3')
                            return '已提交';
                        if (value == '4')
                            return '已导出金税';
                        if (value == '5')
                            return '已导入金税';
                        if (value == '6')
                            return '冲销单';
                    }}

            ]
        ],
        onLoadSuccess: function () {
            $(this).datagrid('selectRow', 0);
            fpcx.setBtnStatus();
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        rowStyler: function (index, row) {
            if (row.sftzd == "是" && row.yjsd_id) {
                return 'background-color:rgba(255, 100, 87, 0.64);';
            }
        },
        onClickRow: function (rowIndex, rowData) {
            fpcx.setBtnStatus();
        },
        onSelect: function (rowIndex, rowData) {
            fpcx.showDetail(rowData);
            fpcx.setBtnStatus();
        }
    });
    //**********按钮触发事件**********

    //查询按钮
    $("#btn_fpcx_search").click(function () {
        fpcx_search();
    });


    //加载查询条件中的申报公司下拉框
    $("#_gs").combotree({
        loadFilter: function (rows) {
            return convert(rows);
        }
    });
});




//查询发票
function fpcx_search() {
    var glqyId = $('#fpcx_glqy').val() == null ? null : $('#fpcx_glqy').val();
    var glqyQc = $('#fpcx_glqy').attr("_text") == null ? null : $('#fpcx_glqy').attr("_text");
    /*   if(glqyQc==""){
     glqyId = null;
     }*/
    var gsId = $('#fpcx_gs').combotree('getValue') == null ? null : $('#fpcx_gs').combotree('getValue');
    var gsJc = $('#fpcx_gs').combotree('getText') == null ? null : $('#fpcx_gs').combotree('getText');
    var fpzlId = $('#fpcx_fpcxzl').combobox('getValue') == null ? null : $('#fpcx_fpcxzl').combobox('getValue');
    var sqrqStart = $('#fpcx_sqrq_start').datebox('getValue');
    var sqrqEnd = $('#fpcx_sqrq_end').datebox('getValue');
   // var zt = $('#fpcx_zt').combobox('getValue') == null ? null : $('#fpcx_zt').combobox('getValue');
   // var type = $('#fpcx_type').combobox('getValue') == null ? null : $('#fpcx_type').combobox('getValue');
    $('#grid_fpcx').datagrid('load',
        {
            jsdh: $('#fpcx_jsdh').val(),
            glqyId: glqyId,
            glqyQc: glqyQc,
            gsId: gsId,
            gsJc: gsJc,
            fpzlId: fpzlId,
            sqrqStart: sqrqStart,
            sqrqEnd: sqrqEnd
            //zt: zt,
          //  type: type
        }
    );
}

function  getFirstDayofMonth(year,month) {
    var new_year = year;    //取当前的年份
    var new_month = month;//取下一个月的第一天，方便计算（最后一天不固定）
    var new_date = new Date(new_year,new_month,1);                //取当年当月中的第一天
    return new_date;
}

function getFirstDayofNextMonth(year,month) {
    var new_year = year;    //取当前的年份
    var new_month = month++;//取下一个月的第一天
    if(month>12) {
        new_month -=12;        //月份减
        new_year++;            //年份增
    }
    var new_date = new Date(new_year,new_month,1); //取当年当月中的第一天
    return new_date;
}
